package snafu.implementation.actions.walk;

import java.util.List;

import snafu.framework.actions.IntentionItself;
import snafu.framework.actions.IntentionView;
import snafu.framework.agents.AgentState;
import snafu.framework.objects.WorldObject;
import snafu.framework.view.Access;
import snafu.framework.worlds.Position;
import snafu.framework.worlds.WorldMap;
import snafu.implementation.states.literals.walk.WalkableLiteral;

public class WalkIntentionItself extends IntentionItself<WalkAction> implements WalkIntention {
	public WalkIntentionItself(Access privacy) {
		super(privacy);
	}

	@Override
	public WalkAction createAction(List<WorldObject> targets) {
		return new WalkAction(targets, this);
	}
	
	@Override
	public IntentionView<WalkAction> createView(Access access) {
		return new WalkIntentionView(access, this);
	}

	@Override
	protected boolean checkApplicability(WorldMap perception, AgentState agent, List<WorldObject> targets) {
		// get the only target
		WorldObject target = targets.get(0);
		
		// get target position
		Position position = perception.getPosition(target);
		if (position == null)
			return false;
		
		// if there is at least one non-walkable object, return false
		for (WorldObject object : perception.get(position).getWorldObjects()) {
			boolean walkable = object.getState().getLiteral(WalkableLiteral.class, true);
			if (!walkable) {
				return false;
			}
		} // TODO why this? if I want to step on grass the ape on it don't have to be walkable
		
		// check if the target is currently walkable
		return target.getState().getLiteral(WalkableLiteral.class, false);
	}

}
